<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%@include file="../../system/sys_common_head.jsp"%>
    <title>Title</title>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input name="name" value="${preRegistration.name}" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">身份证</label>
                    <div class="layui-input-block">
                        <input name="idCard" value="${preRegistration.idCard}"  class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">报名序号</label>
                    <div class="layui-input-block">
                        <input name="graduateNo" value="${preRegistration.graduateNo}"  class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">学校</label>
                    <div class="layui-input-block">
                        <select id="schoolId" name="schoolId" lay-filter="schoolId" lay-verify="required" required>
                            <option value="1" <c:if test="${preRegistration.schoolId == 1}">selected</c:if>>江西赣州技师学院</option>
                            <option value="2" <c:if test="${preRegistration.schoolId == 2}">selected</c:if>>赣州农业学校</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">层级</label>
                    <div class="layui-input-block">
                        <select id="levelId" name="levelId" lay-filter="levelId" lay-verify="required" required>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">专业</label>
                    <div class="layui-input-block">
                        <select id="majorId" name="majorId" lay-filter="majorId" lay-verify="required" required>
                            <c:forEach var="major" items="${majorList}">
                                <option value="${major.id}" <c:if test="${preRegistration.majorId == major.id}">selected</c:if>>${major.majorName}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <input type="hidden" name="id" value="${preRegistration.id}">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="role-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery'], function () {
        let form = layui.form;
        let $ = layui.jquery;

        // 初始化层级
        window.initLevel = function(schoolId){
            $.ajax({
                url: '<%=request.getContextPath()%>/common/queryLevelBySchoolId',
                dataType: 'json',
                data:{schoolId:schoolId},
                type: 'get',
                success: function (result) {
                    var inHtml = "";
                    for(var i = 0;i < result.length; i++){
                        if(result[i].id == ${preRegistration.levelId}){
                            inHtml = inHtml+"<option value='"+result[i].id+"' selected>"+result[i].levelName+"</option>"
                        }else{
                            inHtml = inHtml+"<option value='"+result[i].id+"'>"+result[i].levelName+"</option>"
                        }
                    }
                    $("#levelId").html(inHtml);
                    form.render('select'); //刷新select选择框渲染
                }
            })
        }

        // 初始化专业
        window.initMajor = function(levelId){
            $.ajax({
                url: '<%=request.getContextPath()%>/common/queryMajorByLevelId',
                dataType: 'json',
                data:{levelId:levelId},
                type: 'get',
                success: function (result) {
                    majorList = result;
                    var inHtml = "";
                    for(var i = 0;i < result.length; i++){
                        // inHtml = inHtml+"<option value='"+result[i].id+"'>"+result[i].majorName+"</option>"

                        if(result[i].id == ${preRegistration.majorId}){
                            inHtml = inHtml+"<option value='"+result[i].id+"' selected>"+result[i].majorName+"</option>"
                        }else{
                            inHtml = inHtml+"<option value='"+result[i].id+"'>"+result[i].majorName+"</option>"
                        }
                    }
                    $("#majorId").html(inHtml);
                    form.render('select'); //刷新select选择框渲染
                }
            })
        }

        // 初始化专业
        window.initMajorBySchoolId = function(schoolId){
            $.ajax({
                url: '<%=request.getContextPath()%>/common/queryMajorBySchoolId',
                dataType: 'json',
                data:{schoolId:schoolId},
                type: 'get',
                success: function (result) {
                    var inHtml = "";
                    // majorList = result;
                    for(var i = 0;i < result.length; i++){

                        if(result[i].id == ${preRegistration.majorId}){
                            inHtml = inHtml+"<option value='"+result[i].id+"' selected>"+result[i].majorName+"</option>"
                        }else{
                            inHtml = inHtml+"<option value='"+result[i].id+"'>"+result[i].majorName+"</option>"
                        }
                    }
                    $("#majorId").html(inHtml);
                    form.render('select'); //刷新select选择框渲染
                }
            })
        }

        initLevel(${preRegistration.schoolId})
        initMajor(${preRegistration.levelId})

        form.on('select(schoolId)', function(data){
            initLevel($("#schoolId").val())
        });

        form.on('select(levelId)', function(data){
            initMajor($("#levelId").val())
        });

        form.on('submit(role-save)', function (data) {
            $.ajax({
                url: '<%=request.getContextPath()%>/student/preregistration/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("preRegistration-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>

</body>
</html>